<?php

	//A generic class that contains common functions to (for instance) select and having

	abstract class fluxDbCommon {

		  public function isComparisonOperator ($condition=NULL){

					 if (is_null($condition))
								return;

					 switch ($condition){
						 case '>' :
						 case '>=':
						 case '<' :
						 case '<=':
						 case '=' :
									return $condition;
									break;

						 default:
									//die('Invalid comparison operator ->' . $condition . '<-' . "\n");
									//$this->throwError ('Invalid comparison operator specified');
									return FALSE;
									break;

					 }

		  }


		  public function prepareTables ($tables=NULL){

			  foreach ($tables as $k => $v){

					if (is_array($v)){

						$x = array();
	 					foreach ($v as $key=>$value) 
								  $x[$key]=array($key=>$value);
						continue;

						return ($x);

					}

					return $v;

			  }


		  }

		  public function prepareQuery ($a=NULL){

				foreach ($a as $k=>$v)
						  $a[$k] = (preg_match('#^\!#',$v)) ? ' NOT ' . preg_replace('#^\!#','',$a[$k]) : $a[$k];

				return trim($a['condition'] . ' ' . $a['field'] . ' ' . $a['comparison'] . ' ' . $a['value']) . ' ';

		  }

		  public function parseWherePart ($where){

				$query=NULL;

				foreach ($where as $k => $v){

					if (method_exists ($this,'SQL'.$v['condition'])){
						$query.=call_user_func (array($this,'SQL'.$v['condition']),$v);
						continue;
					}

					$query.=$this->prepareQuery($v);


				}

				return ($query);

		  }

		  public function parsePart($part,$var){

				$query=strtoupper($part) . ' ';

				foreach ($var as $k => $v){

						  if (!is_array ($v)) {
									 $query.=$v . ', ';
									 continue;
						  }

						  foreach ($v as $key=>$value)
							  $query .= $key . ' AS ' . $value . ', ';

				}

				$query  = substr ($query,0,-2);

				$query .= ' ';

				return $query;
					
		  }

		  public function setWhere ($operator,$args) {

				return array(
						  'condition'  => strtoupper($operator), //Boolean operator or where or ...
						  'field'      => (isset($args[0])) ? $args[0] : ' ',
						  'comparison' => (isset($args[1])) ? $args[1] : NULL, //comparison operator
                    'value'      => (isset($args[2])) ? $args[2] : NULL
					);


		  }
		
		  public function isNullField ($field,$method){

				if (is_null($field)||empty($field))
					$this->throwError('fluxCommon:A field must be specified!,in '.$method);

		  }

		  public function parseFields ($fields){

					 if (!is_array($fields))
								$fields=func_get_args();

					 $z = array();

					 while (list($k,$v) = each ($fields)) {

				  		if (!is_int($k)){
							$this->isNullField($k,__METHOD__);
							$z[$k]=array('field'=>$k,'as'=>$v);
						} else {
							$this->isNullField($v,__METHOD__);
							$z[]=$v;
						}

					 }

					 return $z;

		  }

		  private function SQLGroup ($field) {

				$orderAscDesc=NULL;

				if(!is_array($field['field']))
						  return ' GROUP BY ' . $field['field'] . ' ' . $orderAscDesc;


				foreach ($field['field'] as $k=>$v)
						  $ret.=$v . ',';

				$ret = substr($ret,0,-1);

				return ' GROUP BY ' . $ret . ' ' . $orderAscDesc;

		  }

		  private function SQLin ($f){

					 $obj = is_object($f['comparison']) ? $f['comparison'] : $f['field'];

					 if (!is_object($obj)){
								$this->throwError('Expected an object as argument');

				}

				return ' IN ('.$obj.') ';

		  }

		  private function SQLLimit ($field){

				$limit=$field['field'];

				if (!is_int ($limit))
					$this->mainObj->throwError('LIMIT Clause should be an INTEGER ->' .var_dump($limit) . '<- specified' );

				if (!$field['comparison'])
						  return ' LIMIT ' . $limit;

				if (!is_int ($field['comparison']))
					$this->mainObj->throwError('LIMIT Clause should be an INTEGER ->' .var_dump($limit) . '<- specified' );

				return ' LIMIT ' . $limit . ',' . $field['comparison'] . ' ';

		  }


		  private function SQLLike ($field) {
					 $value=preg_replace ('#\*#','%',"'".$field['comparison']."'");
					 return $field['field'] . ' LIKE ' . $value . ' ';
		  }

		  private function SQLOrder ($field) {

				$orderAscDesc=NULL;

				if ($field['comparison']){

					switch (strtoupper($field['comparison'])) {

						case 'ASC' :
						case 'DESC':
								  $orderAscDesc=$field['comparison'];
								  break;
						default:
								  $this->mainObject->throwError('Order By takes ASC or DESC as modifiers');
								  break;

					}

				}

				$ret=NULL;

				if(!is_array($field['field']))
						  return ' ORDER BY ' . $field['field'] . ' ' . $orderAscDesc;


				foreach ($field['field'] as $k=>$v)
						  $ret.=$v . ',';

				$ret = substr($ret,0,-1);

				return ' ORDER BY ' . $ret . ' ' . $orderAscDesc;

		  }

		  private function isNullTable ($table,$method){
	 
				if (is_null($table)||empty($table))
					$this->throwError('fluxSelect:A table must be specified!,in '.$method);

		  }

		  private function isBooleanOperator($operator=NULL){

					 switch(strtolower($operator)){
						 case 'and':
						 case 'or':
						 case 'not':
									return TRUE;
									break;
					 }

					 return FALSE;

		  }

		  public function orderBy ($fieldList) {

				$args=func_get_args();

				foreach ($args as $argv) 
					$this->orderBy[]=$fieldList;

		  }


	}//EOC

?>
